package com.example.mobile0123456789hwh0421;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

//    2、适配器类继承RecyclerView.Adapter，并设置泛型为刚定义的ViewHolder。
public class StudentRVAdapter extends RecyclerView.Adapter<StudentRVAdapter.ViewHolder> {
//    4、定义数据接口，并在构造方法中赋值
    private List<Student> list;
    public StudentRVAdapter(List<Student> list) {
        this.list = list;
    }

    //    1、定义ViewHolder静态内部类，继承RecyclerView.ViewHolder，用于描述列表项文件的控件
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvName,tvAge,tvGrade,tvPhone;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_item_name);
            tvAge = itemView.findViewById(R.id.tv_item_age);
            tvGrade = itemView.findViewById(R.id.tv_item_grade);
            tvPhone = itemView.findViewById(R.id.tv_item_phone);
        }
    }
//   3、添加需要实现的抽象方法
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//      6、获取列表布局文件，然后创建ViewHolder对象
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_rv_main_student, parent, false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//        7、绑定数据，通过positiong获取数据，然后赋值给控件
        Student student = list.get(position);
        holder.tvName.setText(student.getName());
        holder.tvAge.setText(String.valueOf(student.getAge()));
        holder.tvGrade.setText(student.getGrade());
        holder.tvPhone.setText(student.getPhone());
    }
    @Override
    public int getItemCount() {
//      5、获取数据长度
        return list.size();
    }
}